﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.ObjectModel;
using System.Data.Objects.DataClasses;


namespace CCF20Model
{
    public class VehicleMessageData
    {
        public ServiceRequest MyServiceRequest { get; set; }
        public IEnumerable<Fault> MyFaults { get; set; }
        public IEnumerable<ContactHistory> MyContactHistory { get; set; }
    }
    public class ServiceRequestCollection : ObservableCollection<VehicleMessageData>
    {
        private CCF20Entities ccf20Entities;


        protected override void InsertItem(int index, VehicleMessageData item)
        {
            base.InsertItem(index, item);
            
        }

        protected override void RemoveItem(int index)
        {
            base.RemoveItem(index);
        }
        public ServiceRequestCollection(IQueryable<VehicleMessageData> serviceRequest, CCF20Entities context)
            : base(serviceRequest)
        {
            ccf20Entities = context;
        }

    }
    public class VehicleMessageModel
    {
        CCF20Entities ccf20Entities;
        ServiceRequestCollection myServiceRequestCollection;
        public ServiceRequestCollection GetUnHandledServiceRequest()
        {
            var queryService = from s in ccf20Entities.ServiceRequests
                               //where s.ServiceStatus == "未联系"
                               orderby s.ServiceStartTime descending
                               select new VehicleMessageData
                               {
                                   MyServiceRequest = s,
                                   MyFaults = s.Faults.Where(f=>f.FaultType=="故障"),
                                   MyContactHistory = s.ContactHistories
                               };

            myServiceRequestCollection = new ServiceRequestCollection(queryService, ccf20Entities);
            return myServiceRequestCollection;
        }
        public void AddContactHistoryForServiceRequest(ServiceRequest sr, ContactHistory ch)
        {
            ccf20Entities.ContactHistories.AddObject(ch);
            ch.ServiceRequest.LastContactResult = ch.ContactResult;
            ccf20Entities.SaveChanges();
        }
        public VehicleMessageModel()
        {
            ccf20Entities = new CCF20Entities();

        }
        public void CreateServiceRequestByFault(Fault fault)
        {
            ServiceRequest service = null;
            fault.Vehicle = GetVehicleBySimNumber(fault.IDCSimNumber);

            if (fault.FaultType == "撞车")
            {
                service = new ServiceRequest();
                service.ServiceType = Enum.GetName(typeof(EnumServiceType), EnumServiceType.撞车);
            }
            else if (fault.FaultType == "故障")
            {
                service = GetServiceRequestByVehicle(fault.Vehicle);
                service.ServiceType = Enum.GetName(typeof(EnumServiceType), EnumServiceType.故障);
            }
            service.ServiceStartTime = DateTime.Now;
            service.Vehicle = fault.Vehicle;
            service.Device = fault.Vehicle.Devices.FirstOrDefault();
            service.Customer = fault.Vehicle.Customer;
            service.LastContactResult = Enum.GetName(typeof(EnumServiceRequestStatus), EnumServiceRequestStatus.未联系);

            fault.ServiceRequest = service;
            
            ccf20Entities.Faults.AddObject(fault);
            ccf20Entities.SaveChanges();
            
        }
        public ServiceRequest GetServiceRequestByVehicle(Vehicle v)
        {
            string strServiceStatus0 = Enum.GetName(typeof(EnumServiceRequestStatus), EnumServiceRequestStatus.未联系);
            string strServiceStatus1 = Enum.GetName(typeof(EnumServiceRequestStatus), EnumServiceRequestStatus.联系中);
            string strServiceType = Enum.GetName(typeof(EnumServiceType), EnumServiceType.撞车);


            ServiceRequest service = ccf20Entities.ServiceRequests.Where
                (s =>
                    (s.VehicleID == v.VehicleID //Find same vehicle's existing service request
                        && (
                                s.ServiceStatus == strServiceStatus0 ||
                                s.ServiceStatus == strServiceStatus1
                            )
                        // ServiceType=0 urgent. For urgent, we create new service request whenever we receive a crash message.
                        && s.ServiceType != strServiceType
                     )
                  ).FirstOrDefault();
            if (service == null)
            {
                service = new ServiceRequest(); //If you don't the pervious non closed request, then create a new service request.
                service.ServiceStatus = Enum.GetName(typeof(EnumServiceRequestStatus), EnumServiceRequestStatus.未联系);
                ccf20Entities.ServiceRequests.AddObject(service);
                VehicleMessageData  vmd = new VehicleMessageData();
                vmd.MyServiceRequest = service;
                vmd.MyFaults = service.Faults;
                ((IList)myServiceRequestCollection).Add(vmd);
            }
            return service;
        }
        public Vehicle GetVehicleBySimNumber(string SimNumber)
        {
            Vehicle vehicle = ccf20Entities.Vehicles.Where(v => v.Devices.Count(d => d.IDCSimNumber == SimNumber) == 1).FirstOrDefault();
            if (vehicle == null)
            {
                throw new Exception(string.Format("没有找到SIM卡号为{0}的车辆", SimNumber));

            }
            return vehicle;
        }
        public DateTime GetLastCrashTime()
        {
            return ccf20Entities.Faults.Where(f => f.FaultType == "撞车").Max(f => f.HappenDate);
        }
        public void GiveUpCustoemr(ServiceRequest sr)
        {
            ServiceRequest serviceRequest = ccf20Entities.ServiceRequests.FirstOrDefault(s => s.ServiceRequestID == sr.ServiceRequestID);
            if (serviceRequest != null)
            {
                serviceRequest.ServiceStatus = Enum.GetName(typeof(EnumServiceRequestStatus), EnumServiceRequestStatus.放弃);
            }
            ccf20Entities.SaveChanges();
        }
    }
}
